#!/bin/bash
mkdir netmaker;cd netmaker;
curl -O https://gitee.com/zerohacker/other-downloads/raw/master/linux/Centos_x86_64/7/wireguard-vpn/kmod-wireguard-1.0.20220627-1.el7_9.elrepo.x86_64.rpm
curl -O https://gitee.com/zerohacker/other-downloads/raw/master/linux/Centos_x86_64/7/wireguard-vpn/qrencode-3.4.1-3.el7.x86_64.rpm
curl -O https://gitee.com/zerohacker/other-downloads/raw/master/linux/Centos_x86_64/7/wireguard-vpn/wireguard-tools-1.0.20210914-1.el7.x86_64.rpm
yum install *.rpm -y
rm -rf *.rpm
modprobe wireguard
#docker
cat > /etc/yum.repos.d/docker-ce.repo << EOF
[docker-ce-stable]
name=Docker CE Stable
baseurl=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/\$releasever/\$basearch/stable
enabled=1
gpgcheck=0
gpgkey=https://mirrors.ustc.edu.cn/docker-ce/linux/centos/gpg
EOF

cat > /etc/yum.repos.d/epel.repo << EOF
[epel]
name=Extra Packages for Enterprise Linux 7 - \$basearch
baseurl=https://mirrors.ustc.edu.cn/epel/7/\$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
EOF

yum clean all
yum makecache

yum install git jq bind-utils docker-ce-20.10.24-3.el7 docker-compose grep gawk bash-completion net-tools telnet chrony vim lsof wget curl lrzsz unzip dos2unix sysstat iotop pciutils traceroute tcpdump psmisc bc ntpdate ntp acpid gcc cmake make sos createrepo logrotate audit rsyslog p7zip polkit sudo sysbench nmap dmidecode sdparm screen tmux zip gem ncurses-devel automake autoconf libtool gcc-c++ initscripts tldr htop ShellCheck the_silver_searcher iperf3 fio openssl boxes strace mtr expect -y

systemctl daemon-reload && systemctl enable docker && systemctl restart docker

cat >> /etc/docker/daemon.json << EOF
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com"
]
}
EOF

systemctl daemon-reload && systemctl restart docker
ntpdate -u aliyum.com
timedatectl set-timezone Asia/Shanghai
hwclock -w

wget -qO nm-quick.sh https://ghproxy.com/https://raw.githubusercontent.com/gravitl/netmaker/master/scripts/nm-quick.sh && sudo chmod +x nm-quick.sh
sed -i 's/https:\/\/github.com\/mikefarah\/yq\/releases/https:\/\/ghproxy.com\/https:\/\/github.com\/mikefarah\/yq\/releases/g' nm-quick.sh
sed -i 's/https:\/\/github.com\/gravitl/https:\/\/ghproxy.com\/https:\/\/github.com\/gravitl/g' nm-quick.sh
sed -i 's/https:\/\/raw.githubusercontent/https:\/\/ghproxy.com\/https:\/\/raw.githubusercontent/g' nm-quick.sh
sed -i '/BASE_URL\/scripts\/nm-certs.sh/a \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ sed -i \"s\/https\/https:\\/\\/ghproxy.com\\/https\/g" "\$SCRIPT_DIR"/nm-certs.sh' nm-quick.sh
sed -i 's/api.\${NETMAKER_BASE_DOMAIN}/api.\${NETMAKER_BASE_DOMAIN}:50005/g' nm-quick.sh
sed -i '/unset INSTALL_TYPE/i\ARCH=\$(uname -m); if [ "\$ARCH" = "x86_64" ]; then ARCH=amd64 ;elif [ "\$ARCH" = "aarch64" ]; then ARCH=arm64 ;else echo "Unsupported architechure" exit 1; fi;' nm-quick.sh
sed -i '/3. install necessary packages/{n;s/install_dependencies/#install_dependencies/;}' nm-quick.sh
sed -i '/BASE_URL\/scripts\/nm-certs.sh/a \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ sed -i \"s\/coredns\\/coredns\/coredns\\/coredns:1.10.1\/g" "\$SCRIPT_DIR"/docker-compose.yml' nm-quick.sh
sed -i '/BASE_URL\/scripts\/nm-certs.sh/a \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ sed -i \"s\/api.\\${NM_DOMAIN}:443\/api.\\${NM_DOMAIN}:50005\/g" "\$SCRIPT_DIR"/docker-compose.yml' nm-quick.sh
sed -i '/BASE_URL\/scripts\/nm-certs.sh/a \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ sed -i \"s\/\$update_NM_DOMAIN\/\$update_NM_DOMAIN:50005\/g" "\$SCRIPT_DIR"/netmaker.env' nm-quick.sh
sed -i '/BASE_URL\/scripts\/nm-certs.sh/a \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ sed -i "\/80:80\/{n;s\/50005:50005\/50005:443\/;}" "\$SCRIPT_DIR"/docker-compose.yml' nm-quick.sh
sed -i '/BASE_URL\/scripts\/nm-certs.sh/a \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ sed -i \"s\/443\/50005\/g" "\$SCRIPT_DIR"/docker-compose.yml' nm-quick.sh
sed -i '/chmod +x \"\$SCRIPT_DIR\"\/nm-certs.sh/{n;s/\"\$SCRIPT_DIR\"\/nm-certs.sh/#\"\$SCRIPT_DIR\"\/nm-certs.sh/;}' nm-quick.sh
sed -i '/using master key/{n;s/\$NETMAKER_BASE_DOMAIN/\$NETMAKER_BASE_DOMAIN:50005/;}' nm-quick.sh
sed -i '/BASE_URL\/scripts\/nm-certs.sh/a \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ sed -i \"s\/\${NM_DOMAIN}\/\$update_NM_DOMAIN:50005\/g" "\$SCRIPT_DIR"/docker-compose.yml' nm-quick.sh
sed -i '/BASE_URL\/scripts\/nm-certs.sh/a \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ update_NM_DOMAIN=\$(cat "\$SCRIPT_DIR"/netmaker.env | grep NM_DOMAIN)' nm-quick.sh
mkdir certs;
echo "mv pem and key file to certs/: fullchain.pem or privkey.pem"
